<?php

define('QDB_STATUS_DELETED', 2);

if(!isset($header))
	$header = 'No title, call Sikon';
	
if(!isset($mode))
	$mode = null;

if(isset($paginator))
    $paginator->options(array('url' => Sanitize::html($this->passedArgs)));

?>
<h1>
	<?php echo Sanitize::html($header); ?>
</h1>
<?php
	if(!isset($quotes) || !$quotes || $quotes == array())
	{
		echo "No quotes to display";
	}
	else
	{
		if(isset($paginator))
		{
		    echo $this->renderElement('quotenav');
		}
	
		if($mode == 'unapproved') {
?>
			<form id="quote-approve-form" action="<?php echo $html->url('/admin/approve') ?>" method="post">
<?php
		}
		
		foreach ($quotes as $quote) {
	
		$id = $quote['Quote']['id'];
		$tags = trim($quote['Quote']['tags']);
		$score = $quote['Quote']['score'] > 0 ? '+' . $quote['Quote']['score'] : $quote['Quote']['score'];
		
		if($tags == '')
		{
			$tags = array();
		}
		else
		{
			$tags = explode(' ', trim($quote['Quote']['tags']));
			sort($tags);
		}
			
?>
	    <div id="q<?php echo $id; ?>" class="quote">
	    	<div class="quote-header">
		<span class="quote-wait" id="qwait<?php echo $id; ?>">
		    <?php echo $html->image('waitblue.gif', array('alt' => 'w')); ?>
		</span>
		<span class="quote-message" id="qmsg<?php echo $id; ?>">
		</span>
                <?php if($id == -1) { ?>
                    <span class="quote-id">Quote preview</span>
                <?php } else { ?>
                    <span class="quote-id">
                        <a href="<?php echo $html->url('/' . $id); ?>">#<?php echo $id; ?></a>
                    </span>
                <?php if($quote['Quote']['status'] == QUOTE_STATUS_NORMAL) { ?>
                    <span class="quote-stat">
                        Score: <span class="quote-score"><?php echo $score ?></span>/<span class="quote-vcount"><?php echo $quote['Quote']['votes'] ?></span>
                    </span>
                    <span class="quote-vote quote-vote-up">
                        <a href="<?php echo $html->url('/quotes/vote/' . $id . '/up'); ?>" onclick="vote(<?php echo $id ?>, 'up'); return false;"><?php echo $html->image('thumbsup.png', array('alt' => 'I like it', 'title' => 'I like it')); ?></a>
                    </span>
                    <span class="quote-vote quote-vote-down">
                        <a href="<?php echo $html->url('/quotes/vote/' . $id . '/down'); ?>" onclick="vote(<?php echo $id ?>, 'down'); return false;"><?php echo $html->image('thumbsdown.png', array('alt' => "I don't like it", 'title' => "I don't like it")); ?></a>
                    </span>
                <?php } ?>
                    <?php if($isAdmin) { ?>
                        <span class="quote-edit">
                            <a href="<?php echo $html->url('/quotes/edit/' . $id); ?>"><?php echo $html->image('edit.png', array('alt' => 'Edit', 'title' => 'Edit')); ?></a>
                        </span>
			<?php if($quote['Quote']['status'] == QUOTE_STATUS_DELETED) { ?>
                        <span class="quote-undelete">
                            <a href="<?php echo $html->url('/quotes/undelete/' . $id); ?>"><?php echo $html->image('undo.png', array('alt' => 'Undelete', 'title' => 'Undelete')); ?></a>
                        </span>
			<?php } else { ?>
                        <span class="quote-delete">
                            <a href="<?php echo $html->url('/quotes/delete/' . $id); ?>" onclick="delquote(<?php echo $id ?>); return false;"><?php echo $html->image('delete.png', array('alt' => 'Delete', 'title' => 'Delete')); ?></a>
                        </span>
			<?php } ?>
                        <span class="quote-spam">
                            <a href="<?php echo $html->url('/quotes/spam/' . $id); ?>" onclick="spamquote(<?php echo $id ?>); return false;"><?php echo $html->image('spam.png', array('alt' => 'Spam', 'title' => 'Spam')); ?></a>
                        </span>
                    <?php } ?>
                    <span class="quote-date"><?php echo gmdate("H:i, j F Y", $quote['Quote']['created']); ?> (UTC)</span>
            <?php } ?>
           </div>
	   <?php
		if($quote['Quote']['author_id'] || ($isAdmin && $quote['Quote']['author_ip'])) {
	   ?>
		    <div class="quote-author">
			Submitted by
			<?php
			    if($quote['Quote']['author_id']) {
			?>
			    <span class="openid-url"><?php echo $quote['Author']['name']; ?></span>
			    <?php /* if($isAdmin && $quote['Quote']['author_ip']) { ?>
				(<span class="quote-ip"><?php echo $quote['Quote']['author_ip']); ?></span>
			    <?php } */ ?>
			<?php
			    } else {
			?>
			    <span class="quote-ip"><?php echo $quote['Quote']['author_ip']; ?></span>
			<?php
			}
			?>
		    </div>
	   <?php
		}
	   ?>
	       <div class="quote-body">
				<?php echo $quote['Quote']['body']; ?>
	       </div>
	       <?php if(trim($quote['Quote']['notes']) != '') { ?>
		    	<div class="quote-notes">
		    		<span class="quote-notes-label">Notes:</span>
		    		<?php echo $quote['Quote']['notes']; ?>
		    	</div>
	    	<?php } ?>
			<?php if(sizeof($tags) > 0) { ?>	       
		       <div class="quote-tags">
		       	<span class="quote-tags-label">Tags (<?php echo sizeof($tags) ?>): </span>
		       	<?php
		       			foreach($tags as $tag)
		       			{
		       				echo '<a href="' . $html->url('/quotes/tag/' . $tag) . '">' . $tag . '</a> ';
		       			}
		       		?>
		       </div>
	       <?php } ?>
	    </div>
		<?php if($mode == 'unapproved') { ?>
			<div class="quote-approve">
				<input type="radio" name="approve[<?php echo $id; ?>]" value="0" /> Approve
				<input type="radio" name="approve[<?php echo $id; ?>]" value="1" /> Discard
				<input type="radio" name="approve[<?php echo $id; ?>]" value="2" checked="checked" /> Do nothing
			</div>
		<?php } ?>
<?php
		}
	if($mode == 'unapproved') { ?>
		<div id="quote-approve-controls">
			<input type="submit" name="approve-all" value="Approve all" />
			<input type="submit" name="discard-all" value="Discard all" />
			<input type="submit" name="update" value="Update database" />
		</div></form>
	<?php }
	}

	if(isset($paginator))
	{
		echo $this->renderElement('quotenav');
	}
?>
